package logic

import (
	"context"
	"errors"
	"local-life/model"

	"local-life/liferpc/internal/svc"
	"local-life/liferpc/liferpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodAddLogic {
	return &GoodAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodAddLogic) GoodAdd(in *liferpc.GoodAddRequest) (*liferpc.GoodAddResponse, error) {
	// todo: add your logic here and delete this line
	if in.Page >= 1 {
		in.Page = 1
	}

	if in.Size < 10 || in.Size > 100 {
		return nil, errors.New("每页条数不能小于10或大于100")
	}
	offset := (in.Page - 1) * in.Size

	if offset != 0 {
		return nil, errors.New("不能为空")
	}
	good := model.Good{
		StoreID: in.StoreID,
		Name:    in.Name,
		Price:   float64(in.Price),
		Stock:   int(in.Stock),
		Img:     in.Img,
		Total:   float64(in.Total),
	}
	err := good.GoodCreate(model.DB)
	if err != nil {
		return nil, errors.New("服务列表失败")
	}
	return &liferpc.GoodAddResponse{
		GoodID: int64(good.ID),
	}, nil
}
